package com.atguigu.gulimall.product.AppController;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.service.AttrAttrgroupRelationService;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.AttrGroupRelationVo;
import com.atguigu.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrGroupService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;



/**
 * 属性分组
 *
 * @author yangheng
 * @email sunlightcs@gmail.com
 * @date 2023-03-25 08:39:04
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;


    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;


    /**
     * 获取  同一分类下 所有的 属性分组 以及属性
     * @param catId
     * @return
     */
   @GetMapping("/{catelogId}/withattr")
   public R getAttrGroupWithAttrs(@PathVariable("catelogId") long catId){
      List<AttrGroupWithAttrsVo>  vos=attrGroupService.getAllAttrWithgroupMessage(catId);
       return  R.ok().put("data",vos);
   }





    /**
     *   新增 分组 关联属性  可能会多个
     * @param vos  集合封装
     * @return
     */
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVo> vos){
       attrAttrgroupRelationService.saveBatchVo(vos);
        return R.ok();
    }





    /**
     *    获取当前  分类 可以进行 属性关联的属性
     * @param attrgroupId
     * @return
     */
    @GetMapping("{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId,
                            @RequestParam Map<String, Object> params){
        PageUtils entities=attrService.getNoRelations(attrgroupId,params);
        return R.ok().put("page",entities);
    }



    /**
     *   前端 传来的数据 为 Json 字符串  {'attrid': ,'attrGroupid':} 需要用  @RequestBody 进行封装
     * @param attrs
     * @return
     */
    @RequestMapping("/attr/relation/delete")
    public R deleteRelations(@RequestBody AttrGroupRelationVo[] attrs){
         attrAttrgroupRelationService.deleteRelations(attrs);
        return R.ok();
    }

    /**
     * 查询 属性分组 所关联的 所有 基本属性
     * @param attrgroupId   属性分组id
     * @return
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId){

      List<AttrEntity> attrs= attrService.getAllRelations(attrgroupId);
      return R.ok().put("data", attrs);
    }




    /**
   * 列表
    */
    @RequestMapping("/list/{categoryId}")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("categoryId")long categoryId){
       // PageUtils page = attrGroupService.queryPage(params);
        // 通过 前端传的 参数
        PageUtils page=  attrGroupService.queryPageByCategoryId(params,categoryId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")

    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        // 现获取到 该属性分组的 分组id
        Long catelogId = attrGroup.getCatelogId();
        Long[] parentsId=categoryService.getParentsById(catelogId);
        attrGroup.setCatelogPath(parentsId);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")

    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")

    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")

    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
